/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmCadastraPagar.java
 *
 * Created on 22/09/2009, 10:17:22
 */
package contaspagar.telas;


import caixa.classes.Caixa;
import contaspagar.bd.ContasPagarDAO;
import contaspagar.classes.ContasPagar;
import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import planocontas.classes.PlanoContas;
import planocontas.telas.FrmConsultaPlanoContas;
import utilitarios.ItelaConsulta;

/**
 *
 * @author Wolverine
 */
public class FrmCadastraPagar extends javax.swing.JFrame implements ItelaConsulta {

    /** Creates new form FrmCadastraPagar */
    public FrmCadastraPagar(ContasPagar contasPagar) {
        initComponents();
        setLocationRelativeTo(null);
        setVisible(true);
        try {
            contasPagarDAO = new ContasPagarDAO();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
            ex.printStackTrace();
        }
        Date dataAtual = new Date (System.currentTimeMillis());
        dataEmissaojDateChooser.setDate(dataAtual);
        this.contasPagar = contasPagar;
        this.caixa = new Caixa();
        this.planoContas = contasPagar.getConta();
        if (planoContas != null && contasPagar != null) {
            populaTela();
        }
        }
    private void populaTela() {
        nomeCedentejTextField.setText(contasPagar.getCedente());
        dataEmissaojDateChooser.setDate(contasPagar.getDataEmissao());
        dataVencimentojDateChooser.setDate(contasPagar.getDataPagamento());
        valorAPagarjFormattedTextField.setValue(contasPagar.getValorPagar());
        observacaojTextField.setText(contasPagar.getObservacao());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cadastraPagarjPanel = new javax.swing.JPanel();
        cadastraPagarDadosjPanel = new javax.swing.JPanel();
        dataEmissaojLabel = new javax.swing.JLabel();
        nomeCedentejTextField = new javax.swing.JTextField();
        planoContasDescricaojTextField = new javax.swing.JTextField();
        observacaojLabel = new javax.swing.JLabel();
        observacaojTextField = new javax.swing.JTextField();
        jToolBar1 = new javax.swing.JToolBar();
        jButton1 = new javax.swing.JButton();
        dataVencimentojDateChooser = new com.toedter.calendar.JDateChooser();
        dataVencimentojLabel = new javax.swing.JLabel();
        valorAPagarjLabel = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        nomeCedentejLabel = new javax.swing.JLabel();
        dataEmissaojDateChooser = new com.toedter.calendar.JDateChooser();
        valorAPagarjFormattedTextField = new javax.swing.JFormattedTextField();
        numeroContajFormattedTextField = new javax.swing.JFormattedTextField();
        numeroContajLabel = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        salvarTelajButton = new javax.swing.JButton();
        fecharTelajButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Contas a Pagar");
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        URL url = this.getClass().getResource("/imagens/logo_mini.png");
        Image imagemTitulo = Toolkit.getDefaultToolkit().getImage(url);
        this.setIconImage(imagemTitulo);
        try{
            UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
        }catch (Exception e){
            e.printStackTrace();
        }
        cadastraPagarjPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        cadastraPagarjPanel.setPreferredSize(new java.awt.Dimension(800, 600));
        cadastraPagarjPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        cadastraPagarDadosjPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        dataEmissaojLabel.setText("Data Emissão");
        cadastraPagarDadosjPanel.add(dataEmissaojLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 13, -1, -1));
        cadastraPagarDadosjPanel.add(nomeCedentejTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 70, 370, -1));

        planoContasDescricaojTextField.setEditable(false);
        cadastraPagarDadosjPanel.add(planoContasDescricaojTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 110, 340, -1));

        observacaojLabel.setText("Observação");
        cadastraPagarDadosjPanel.add(observacaojLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 173, -1, -1));
        cadastraPagarDadosjPanel.add(observacaojTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 190, 370, -1));

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/magnifier.png"))); // NOI18N
        jButton1.setToolTipText("Pesquisa Plano de Contas");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                consultaPlanoContas(evt);
            }
        });
        jToolBar1.add(jButton1);

        cadastraPagarDadosjPanel.add(jToolBar1, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 110, 30, 20));
        cadastraPagarDadosjPanel.add(dataVencimentojDateChooser, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 150, 100, -1));

        dataVencimentojLabel.setText("Data Vencimento");
        cadastraPagarDadosjPanel.add(dataVencimentojLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 133, -1, -1));

        valorAPagarjLabel.setText("Valor a Pagar");
        cadastraPagarDadosjPanel.add(valorAPagarjLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 133, -1, -1));

        jLabel4.setText("Plano de Contas");
        cadastraPagarDadosjPanel.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 93, -1, -1));

        nomeCedentejLabel.setText("Cedente");
        cadastraPagarDadosjPanel.add(nomeCedentejLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 53, -1, -1));
        cadastraPagarDadosjPanel.add(dataEmissaojDateChooser, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 30, 100, -1));

        valorAPagarjFormattedTextField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#,##0.00"))));
        cadastraPagarDadosjPanel.add(valorAPagarjFormattedTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 150, 100, -1));
        cadastraPagarDadosjPanel.add(numeroContajFormattedTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 30, 80, -1));

        numeroContajLabel.setText("Nº Conta");
        cadastraPagarDadosjPanel.add(numeroContajLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 13, -1, -1));

        cadastraPagarjPanel.add(cadastraPagarDadosjPanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 400, 230));

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel3.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        salvarTelajButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/tick.png"))); // NOI18N
        salvarTelajButton.setText("Salvar");
        salvarTelajButton.setToolTipText("Salvar Contas a Pagar");
        salvarTelajButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                salvarTelajButtonActionPerformed(evt);
            }
        });
        jPanel3.add(salvarTelajButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 8, -1, -1));

        fecharTelajButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/cross.png"))); // NOI18N
        fecharTelajButton.setText("Fechar");
        fecharTelajButton.setToolTipText("Fechar");
        fecharTelajButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fecharTelajButtonActionPerformed(evt);
            }
        });
        jPanel3.add(fecharTelajButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 8, -1, -1));

        cadastraPagarjPanel.add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 240, 370, 40));

        getContentPane().add(cadastraPagarjPanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 420, 300));

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void consultaPlanoContas(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_consultaPlanoContas
        new FrmConsultaPlanoContas(this);
    }//GEN-LAST:event_consultaPlanoContas
    private void fecharTelajButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fecharTelajButtonActionPerformed
        this.dispose();
    }//GEN-LAST:event_fecharTelajButtonActionPerformed
    private void salvarTelajButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_salvarTelajButtonActionPerformed
        try{
        this.contasPagar.setIdConta(numeroContajFormattedTextField.getText());
        this.contasPagar.setCedente(nomeCedentejTextField.getText());
        this.contasPagar.setDataEmissao(dataEmissaojDateChooser.getDate());
        this.contasPagar.setConta(planoContas);
        this.contasPagar.setDataPagamento(dataVencimentojDateChooser.getDate());
        this.contasPagar.setValorPagar(Double.parseDouble(valorAPagarjFormattedTextField.getValue().toString()));
        this.contasPagar.setObservacao(observacaojTextField.getText());
        this.caixa.setIdCaixa(0);
        }catch (Exception ex){
            ex.printStackTrace();
        }
        try{
        contasPagarDAO.salvar(contasPagar);
        JOptionPane.showMessageDialog(this,"Dados salvos com sucesso");
        }catch (Exception ex){
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this,"Dados não foram salvos");
        }
    }//GEN-LAST:event_salvarTelajButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel cadastraPagarDadosjPanel;
    private javax.swing.JPanel cadastraPagarjPanel;
    private com.toedter.calendar.JDateChooser dataEmissaojDateChooser;
    private javax.swing.JLabel dataEmissaojLabel;
    private com.toedter.calendar.JDateChooser dataVencimentojDateChooser;
    private javax.swing.JLabel dataVencimentojLabel;
    private javax.swing.JButton fecharTelajButton;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JLabel nomeCedentejLabel;
    private javax.swing.JTextField nomeCedentejTextField;
    private javax.swing.JFormattedTextField numeroContajFormattedTextField;
    private javax.swing.JLabel numeroContajLabel;
    private javax.swing.JLabel observacaojLabel;
    private javax.swing.JTextField observacaojTextField;
    private javax.swing.JTextField planoContasDescricaojTextField;
    private javax.swing.JButton salvarTelajButton;
    private javax.swing.JFormattedTextField valorAPagarjFormattedTextField;
    private javax.swing.JLabel valorAPagarjLabel;
    // End of variables declaration//GEN-END:variables
    private PlanoContas planoContas;
    private ContasPagar contasPagar;
    private ContasPagarDAO contasPagarDAO;
    private Caixa caixa;

    public void setData(Object objeto) {
        this.planoContas = (PlanoContas) objeto;
        planoContasDescricaojTextField.setText(planoContas.getDescricao());
    }
}
